var galleryStarter;

var isGalleryOn = true;

var counter = 0;
var imgAdmin;
var imgGuest
var timerOpen;
var timerClose;
var log;
var name;

var animatingOpen = 0;
var animatingClose = 0;

var openTil = 125;

onload = init;

function init() {

	log = document.getElementById("log");
	
	var r=authenticateUser();
	if (r) {
		window.location = ('http://ydev.netii.net/home.html');
	}

	imgAdmin = document.getElementById("imgAdmin");

	imgGuest = document.getElementById("imgGuest");

	imgAdmin.onclick = function() {
		startAnimation("imgAdmin");
	}//function () { setInterval("startGallery()", 1) };
	imgGuest.onclick = function() {
		startAnimation("imgGuest");
	}
	log.innerHTML = "Woreks";

}

function submitClicked() {
	var adminName = $("#adminName");
	var string = $("#adminName").val();
	if (string.length <= 0) {
		// If null display and alert box
		alert("Please enter your user name");
		// Place the cursor on the field for revision
		$("#adminName").focus();
		// return false to stop further processing
		return (false);
	}
	string = $("#adminPassword").val();
	if (string.length <= 0) {
		// If null display and alert box
		alert("Please enter your passoword");
		// Place the cursor on the field for revision
		$("#adminPassword").focus();
		// return false to stop further processing
		return (false);
	} else {

		var params = new Array();
		params['action'] = "login";
		params['userName'] = $("#adminName").val();
		params['userPassword'] = $("#adminPassword").val();
		var param = createURLParams(params);
		var URL = server + "?" + param;
		var request = myCall(URL);

		request.done(function(result) {
			var obj = jQuery.parseJSON(result);
			if (obj.status == 1) {
				window.location = (obj.reUrl);
			}
			alert(result);
			//return request;
		});
		request.fail(function(jqXHR, textStatus) {
			alert("Request failed: " + textStatus);
		});

		//alert(result);
	}
}

function startAnimation(name) {
	if (animatingOpen != 0 || animatingClose != 0) {
		return;
	}
	var div = document.getElementById("guestForm");
	var h = parseInt(div.offsetHeight);
	log.innerHTML = "strarted amit,easd";
	if (name == "imgAdmin" && h > 0) {
		log.innerHTML = "Worasdaseks";
		timerClose = setInterval(function() {
			closeForm("imgGuest")
		}, 20);
		timerOpen = setInterval(function() {
			openForm(name)
		}, 20);
	} else if (name == "imgAdmin" && h <= 0) {
		log.innerHTML = "Worasdaseks";
		if (parseInt(document.getElementById("adminForm").offsetHeight) >= openTil) {
			timerClose = setInterval(function() {
				closeForm(name)
			}, 20);
		} else {
			timerOpen = setInterval(function() {
				openForm(name)
			}, 20);
		}
	}

	div = document.getElementById("adminForm");
	h = parseInt(div.offsetHeight);

	if (name == "imgGuest" && h > 0) {
		log.innerHTML = "Worasdaseks";
		timerClose = setInterval(function() {
			closeForm("imgAdmin")
		}, 20);
		timerOpen = setInterval(function() {
			openForm(name)
		}, 20);
	} else if (name == "imgGuest" && h <= 0) {
		log.innerHTML = "Worasdaseks";
		if (parseInt(document.getElementById("guestForm").offsetHeight) >= openTil) {
			timerClose = setInterval(function() {
				closeForm(name)
			}, 20);
		} else {
			timerOpen = setInterval(function() {
				openForm(name)
			}, 20);
		}
	}
}

function openForm(name) {
	animatingOpen = 1;
	var formdiv
	if (name == "imgAdmin")
		formdiv = document.getElementById("adminForm");
	if (name == "imgGuest")
		formdiv = document.getElementById("guestForm");
	var h = parseInt(formdiv.offsetHeight) + 5;
	formdiv.style.height = h + "px";

	if (h > openTil) {
		h = openTil;
		animatingOpen = 0;
		clearInterval(timerOpen)
	}
}

function closeForm(name) {
	animatingClose = 1;
	var formdiv
	if (name == "imgAdmin")
		formdiv = document.getElementById("adminForm");
	if (name == "imgGuest")
		formdiv = document.getElementById("guestForm");
	var h = parseInt(formdiv.offsetHeight) - 5;
	formdiv.style.height = h + "px";

	if (h <= 0) {
		h = 0;
		animatingClose = 0;
		clearInterval(timerClose)
	}
}